/*
 * @Author: ayun chengyunwu@jiatui.com
 * @Date: 2023-07-11 18:07:47
 * @LastEditors: ayun chengyunwu@jiatui.com
 * @LastEditTime: 2023-07-14 16:08:04
 * @FilePath: /ift-economy-network-h5/src/pages/InvestmentScheduling/Home/index.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import projectMonitoring from "@/assets/images/icon_project_monitoring.png";
import warehousingSituation from "@/assets/images/detail_icon.png";
import barImage from "@/assets/image/bar.png";
import { useEffect, useMemo, useState } from "react";
import Styles from "./index.module.scss";
import Cookies from "js-cookie";
import { useNavigate, useSearchParams } from "react-router-dom";
import { getUserDataInfo } from "@/service/projectDetail";
import { Toast } from "antd-mobile";
import { getRsaDecrypt } from '@/utils/entry'

const index = () => {
  // const token = Cookies.get("pai_token");
  const [searchParams] = useSearchParams();
  const token = searchParams?.get("token") ?? null;
  const navigate = useNavigate();

  const getUser = async () => {
    Cookies.set("pai_token", token ?? "");
    try {
      const result: any = await getUserDataInfo({ token: token });
      const res: any = getRsaDecrypt(result?.data)
      const { data, code } = res;
      if (code != 0) return;
      Cookies.set("user_deptName", data?.deptName ?? "");
      Cookies.set("user_realityName", data?.realityName ?? "");
      Cookies.set('user_isAdmin', data?.isAdmin ?? 0)
    } catch (error) {
      console.log(error);
    }
  };

  useEffect(() => {
    getUser();
  }, [token]);

  const initData = useMemo(
    () => ({
      palaceGridData: [
        {
          text: "总体情况",
          url: "/general-situation",
          icon: projectMonitoring,
        },
        {
          text: "招商项目库",
          url: "/project-progress",
          icon: barImage,
        },
        {
          text: "重大招商项目",
          url: `/project-progress?label=2024年重大招商项目&importProject=1`,
          icon: warehousingSituation,
        },
      ],
    }),
    []
  );

  // 处理跳转事件
  const handleNavigate = (data: { url: string }) => {
    if (!Cookies.get("user_deptName"))
      return Toast.show("SecretToken检查，无效的token。");
    const { url } = data;
    navigate(url);
  };

  return (
    <div className={`${Styles.container}`}>
      <ul
        className={`bg_ffffff m_l_r_12 base-content-view ${Styles.palace_grid}`}
      >
        {initData.palaceGridData.map((item) => (
          <li
            key={item.icon}
            className={`d_column_center ${Styles.item}`}
            onClick={() => handleNavigate(item)}
            data-url={item.url}
          >
            <img style={{ width: "56px" }} src={item.icon} />
            <span className={`ff_R fontsize_16 color_333 mt_12 fw_600`}>
              {item.text}
            </span>
          </li>
        ))}
      </ul>
    </div>
  );
};

export default index;
